/**
* Copyright (c) 2010 Tony Zhou (http://www.cnblogs.com/zhouto/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
* version: jquery.groupTable.js on 5/18/2011 
**/
;
(function(jQuery) {
    $.fn.extend({
        groupTable: function(options) {
            var settings = { groupby: "0", collapse: 0, showSummary: 1 };
            $.extend(settings, options);
            return this.each(function() {
                var tb = $(this);
                //var id = tb.attr("id");
                var header = tb.find("thead th"); ;
                var rows = tb.find("tbody tr");
                var groupby = settings.groupby;
                var groups = groupby.split(",");
                var preValues = new Array(groups.length);
                var curValues = new Array(groups.length);
                var keys = new Array(groups.length);
                var keyIndex = 0;
                var columnCount = header.length;
                var rowNum = 0;
                tb.attr("groupby", settings.groupby);
                $.each(groups, function(j, group) {
                    header.eq(group).addClass("_hidden").hide();
                    keys[j] = "";
                });

                tb.find("thead th").eq(0)
                                   .before("<th colspan=" + groups.length + " class='space'></th>");

                $("<div class='minus2'></div>").bind("click", function() {
                    $(this).toggleClass("plus");
                    $(this).parent().parent().parent().next().toggle();
                }).appendTo(tb.find("thead th").eq(0));

                rows.each(function(i, row) {
                    rowNum = rowNum + 1;
                    $.each(groups, function(j, group) {
                        curValues[j] = $(row).find("TD").eq(group).html();
                        if (curValues[j] != preValues[j]) {
                            if (j == 0)
                                $(row).before("<tr id='" + rowNum + "' class='category category_" + j + "'><td colspan=" + columnCount + "><div  class='blank' style='width:" + 9 * j + "px'></div><div  class='minus'></div><div class='blank'></div>" + curValues[j] + "</td></tr>");
                            else
                                $(row).before("<tr id='" + rowNum + "' class='category category_" + j + " " + keys[j - 1] + "'><td colspan=" + columnCount + "><div  class='blank' style='width:" + 9 * j + "px'></div><div  class='minus'></div><div class='blank'></div>" + curValues[j] + "</td></tr>");
                            keys[j] = rowNum;
                            keyIndex = j;
                            rowNum = rowNum + 1;
                            preValues[j] = curValues[j];
                            for (l = j + 1; l < groups.length; l++)
                                preValues[l] = "";
                        }
                        $(row).find("TD").eq(group).addClass("_hidden").hide(); ;
                    });
                    $(row).addClass("" + keys[keyIndex]);
                    $(row).find("TD").eq(0).before("<td colspan=" + groups.length + " class='space'></td>");
                });

                var minus = tb.find(".minus");
                minus.each(function(i, button) {
                    var g = $(this).parent().parent().attr("id");
                    var subcategoryCount = tb.find("." + g).length;
                    if (settings.showSummary == 1) {
                        $("<span class='summary'>(" + subcategoryCount + ")</span>").appendTo($(this).parent());
                    }
                    if (settings.collapse == 1) {
                        $(this).addClass("plus");
                        var g = $(this).parent().parent().attr("id");
                        tb.find("." + g).hide();
                    }
                    $(button).parent().bind("dblclick", function() { $(button).click(); })
                    $(button).click(function() {
                        if ($(this).hasClass("plus")) {
                            $(this).removeClass("plus");
                            var g = $(this).parent().parent().attr("id");
                            var subcategoryCount = tb.find("." + g).length;
                            if (subcategoryCount > 0)
                                tb.find("." + g).find(" TD[colspan=" + columnCount + "]>div").filter(".minus").addClass("plus");
                            tb.find("." + g).show();
                        }
                        else {
                            group_hide(this);
                        }
                    });
                });
                function group_hide(button) {
                    $(button).addClass("plus");
                    var g = $(button).parent().parent().attr("id");
                    tb.find("." + g).hide();
                    var subcategoryCount = tb.find("." + g).length;
                    if (subcategoryCount > 0) {
                        tb.find("." + g).find(" TD[colspan=" + columnCount + "]>div").filter(".minus").each(function() {
                            group_hide(this);
                        });
                    }
                }
            });
        },
        UngroupTable: function() {
            var $groupTable = $(this);
            return this.each(function() {
                if (!$groupTable.attr("groupby"))
                    return false;
                else {                    
                    var groups = $groupTable.attr("groupby").split(",");
                    $groupTable.find(".category").remove();
                    $groupTable.find("thead th").eq(0).remove();
                    var rows = $groupTable.find("tbody tr").each(function(i, row) {
                        $(row).find("td").eq(0).remove();
                    });
                    $groupTable.find("._hidden").show();
                    $groupTable.attr("groupby", "");
                }
            });
        }
    });
})(jQuery);